
<%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.apache.commons.io.output.*" %>
<%@ page import="uit.edu.vn.Service" %>


<%
   File file ;
   int maxFileSize = 20000 * 1024;
   int maxMemSize = 5000 * 1024;
   String path = "";
   ServletContext context = pageContext.getServletContext();

   String filePath = getServletContext().getRealPath("/")+"USERS/";
   // Verify the content type   
   String contentType = request.getContentType();
   System.out.println("=====================request========================");
   System.out.println("request.getAuthType():  " +request.getAuthType());
   System.out.println("request.getCharacterEncoding():  " +request.getCharacterEncoding());
   System.out.println("request.getContentLength():  " +request.getContentLength());
   System.out.println("request.getContentType():  " + request.getContentType());
   System.out.println("request.getContextPath():  " + request.getContextPath());
   System.out.println("request.getLocalAddr():  " + request.getLocalAddr());
   System.out.println("request.getLocalName():  " + request.getLocalName());
   System.out.println("request.getLocalPort():  " + request.getLocalPort());
   System.out.println("request.getMethod():  " + request.getMethod());
   System.out.println("request.getPathInfo():  " + request.getPathInfo());
   System.out.println("request.getPathTranslated():  " + request.getPathTranslated());
   System.out.println("request.getProtocol():  " + request.getProtocol());
   System.out.println("request.getQueryString():  " + request.getQueryString());
   System.out.println("request.getRemoteAddr():  " + request.getRemoteAddr());
   System.out.println("request.getRemoteHost():  " + request.getRemoteHost());
   System.out.println("request.getRemotePort():  " + request.getRemotePort());
   System.out.println("request.getRemoteUser()  " + request.getRemoteUser());
   System.out.println("request.getRequestedSessionId():  " + request.getRequestedSessionId());
   System.out.println("request.getRequestURI():  " + request.getRequestURI());
   System.out.println("request.getScheme():  " + request.getScheme());
   System.out.println("request.getServerName():  " + request.getServerName());
   System.out.println("request.getServerPort():  " + request.getServerPort());
   System.out.println("request.getServletPath():  " + request.getServletPath());
   System.out.println("=====================endrequest========================");
   if ((contentType.indexOf("multipart/form-data") >= 0)) {
	   System.out.println("=====================data========================");
      DiskFileItemFactory factory = new DiskFileItemFactory();
      // maximum size that will be stored in memory
      factory.setSizeThreshold(maxMemSize);
      // Location to save data that is larger than maxMemSize.
      File directory = new File (".");
      String temp = directory.getCanonicalPath();
      System.out.println("temp dir: " + temp);
      factory.setRepository(new File(temp));

      // Create a new file upload handler
      ServletFileUpload upload = new ServletFileUpload(factory);
      // maximum file size to be uploaded.
      upload.setSizeMax( maxFileSize );
      try{ 
         // Parse the request to get file items.
         List fileItems = upload.parseRequest(request);
         // Process the uploaded file items
         Iterator i = fileItems.iterator();
         while ( i.hasNext () ) 
         {
            FileItem fi = (FileItem)i.next();
            if ( !fi.isFormField () )	
            {
	            // Get the uploaded file parameters	            	            	            	            
	            String fieldName = fi.getFieldName();
	            String fileName = fi.getName().trim();
	            fileName = fileName.replaceAll(" ", "@");
	            StringTokenizer st = new StringTokenizer(fieldName,"@");
	            ArrayList<String> list = new ArrayList<String>();
	            while(st.hasMoreTokens()){
	            	list.add(st.nextToken());
	            }
	            int userid = Integer.parseInt(list.get(0).toString());
	            int fileType = Integer.parseInt(list.get(1).toString());
	            String comment = list.get(2).toString();
	            String username = Service.getUsernameById(userid);
	            filePath += username.trim() +"/";
	            File dir = new File(filePath);
	            dir.mkdirs();
	            boolean isInMemory = fi.isInMemory();
	            long sizeInBytes = fi.getSize();
	            // Write the file
	            if( fileName.lastIndexOf("\\") >= 0 ){
		            file = new File( filePath + fileName.substring( fileName.lastIndexOf("\\"))) ;
	            }else{
		            file = new File( filePath + fileName.substring(fileName.lastIndexOf("\\")+1)) ;
	            }
	            //ghi file xuong server theo duong dan
	            fi.write( file ) ;
	            String smg = Service.writeFile(userid, fileName, fileType, (int)fi.getSize(), comment);
	            
	            System.out.println("=====================enddata========================");
	            out.println("1");
	            out.println("upload thanh cong!");
	            System.out.println("file path is: " +filePath +fileName);
	            System.out.println("getName is: " +fi.getName());
	            System.out.println("file name is: " +fileName);
	            System.out.println("field is: " +fieldName);
	            System.out.println("content type is: " +fi.getContentType());
	            System.out.println("Size is: " +fi.getSize());
            }
         }
      }catch(Exception ex) {
         System.out.println(ex);
      }
   }else{
	   
   }
%>